<?php
/**
 * CategoryController.php
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/**
 * BlogAdmin_CategoryController
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */
class BlogAdmin_CategoryController extends Madoqua_Controller_Action_Abstract 
{
    /**
     * category model
     *
     * @var Blog_Category
     */
    private $_categoryModel;
    
    /**
     * initialization
     */
    public function init()
    {
        parent::init();
        $this->_categoryModel = $this->_modelLoader->get('Blog_Category');        
    }
    
    /**
     * categories index
     */
    public function indexAction() 
    {
        $this->view->categories = $this->_categoryModel->fetchAll();
        
        $form = $this->_formLoader->get('BlogAdmin_Category_Category');
        $form->setRouteAction(array('action' => 'create'));
        $this->view->form = $form;
    }
    
    /**
     * create a new category
     */
    public function createAction()
    {
        $form = $this->_formLoader->get('BlogAdmin_Category_Category');
        $form->setRouteAction(array('action' => 'create'));
        $this->view->form = $form;
        //the form
        
        if ($form->isPrg() && $form->isValid($form->getPrgData()))
        {
            //valid form prg-ed
            $category = $this->_categoryModel->fetchNew();
            //the new row
            
            try {
                $category->setFromArray($form->getModelValues($this->_categoryModel));
                //set the values                
                
                $category->save();
                //save it
            } catch (Exception $e) {
                require_once 'Madoqua/Exception.php';
                throw new Madoqua_Exception($e->getMessage(), Madoqua_Exception::WARNING);
            }
            
            $this->getHelper('redirector')->gotoRoute(
                array(
                        'controller' => 'category', 
                        'action' => 'update',
                        'id' => $category->id
                ),
                'blog_admin'
            );
            //redirect to update            
        }
    }
    
    /**
     * update a category
     */
    public function updateAction() 
    {
        $id = (int) $this->getRequest()->getParam('id');
        $category = $this->_categoryModel->fetchById($id);
        
        if (empty($category)) {
            throw new Exception('Category not found');
        }
        
        $form = $this->_formLoader->get('BlogAdmin_Category_Category');
        $form->setRouteAction(array('action' => 'update', 'id' => $category->id));
        $this->view->form = $form;
        //the form
        
        $form->populate($category->toArray());
        //populate the form
        
        if ($form->isPrg() && $form->isValid($form->getPrgData()))
        {
            //valid form prg-ed
            $category->setFromArray($form->getModelValues($this->_categoryModel));
            //set the values
            
            $category->save();
            //save it
            
            $this->view->success = true;
            //woo!
        }
        
    }
}